package com.andova.empty.widget;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.RequiresApi;

import com.andova.empty.api.IEmptyActivityOption;
import com.andova.empty.api.IEmptyPlugin;
import com.andova.manager.AndovaModuleManager;

/**
 * Created by Administrator on 2018-02-08.
 *
 * @author kzaxil
 * @since 1.0.0
 */
public class EmptyLinearLayout extends LinearLayout implements IEmptyPlugin {
    private EmptyViewGroupAgent mAgent;

    public EmptyLinearLayout(Context context) {
        this(context, null);
    }

    public EmptyLinearLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EmptyLinearLayout(IEmptyActivityOption option, Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        setEmptyOption(option);
    }

    public EmptyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public EmptyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        mAgent = new EmptyViewGroupAgent(AndovaModuleManager.obj().empty().appOption());
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mAgent.onFinishInflate(this);
        mAgent.getOptions().emptyStatePage(mAgent.getEmptyView(), this);
        mAgent.getOptions().errorStatePage(mAgent.getErrorView(), this);
        mAgent.getOptions().loadingStatePage(mAgent.getLoadingView(), this);
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        mAgent.addView(child);
    }

    @Override
    public void showEmptyView() {
        mAgent.showEmpty(this);
    }

    @Override
    public void showErrorView() {
        mAgent.showError(this);
    }

    @Override
    public void showLoadingView() {
        mAgent.showLoading(this);
    }

    @Override
    public void showContentView() {
        mAgent.showContent(this);
    }

    @Override
    public void setEmptyOption(IEmptyActivityOption options) {
        mAgent.setEmptyOption(options);
    }
}
